#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2021/3/1
# @Author: Sunny
# ========================

"""
线程多的时候一定要注意避免出现死锁

"""
from threading import  Lock,Thread

n=100
def work1():
    """完成浇花工作"""
    global n
    for i in range(500000):
        lockA.acquire()
        lockB.acquire()
        n+=1
        lockB.release()
        lockA.release()


def work2():
    """完成打墙工作"""
    global n
    for i in range(500000):
        lockB.acquire()
        lockA.acquire()
        n+=1
        lockA.release()
        lockB.release()


lockA=Lock()
lockB=Lock()

t1 = Thread(target=work1)
t2 = Thread(target=work2)

t1.start()
t2.start()
t1.join() #默认等待子线程1执行结束
t2.join()
print(n)
